package com.eskie.vmobileapp;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.telephony.SmsManager;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CustomDialog extends Dialog {

	Button btn_ok;
	Button btn_cancel;
	Context context;
	String name;
	String msg;
	public static String IDNumber = "ID Number";
	public static String Gateway = "Gateway";
	public static String PIN = "PIN";
	public static String CPNumber = "Cellphone Number";
	public static String Amount = "Amount";
	SharedPreferences.Editor edit;
	public static String conf_pref = "MyPref";

	protected CustomDialog(Context context, String name) {
		super(context);
		this.context = context;
		this.name = name;
	}

	public void start() {
		SharedPreferences pref = context.getSharedPreferences(conf_pref, 0);
		edit = pref.edit();
		final Dialog dialog = new Dialog(context);
		dialog.setContentView(R.layout.custom_dialog);
		dialog.setTitle(name);
		final EditText txtVal = (EditText) dialog.findViewById(R.id.number);
		btn_ok = (Button) dialog.findViewById(R.id.btn_submit);
		btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
		if (name.equals(IDNumber))
			txtVal.setHint("Enter ID");
		else if (name.equals(Gateway) || name.equals(PIN)
				|| name.equals(Amount) || name.equals(CPNumber))
			txtVal.setHint("Enter Number");
		if (name.equals(PIN))
			txtVal.setTransformationMethod(PasswordTransformationMethod
					.getInstance());
		btn_ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (name.equals(CPNumber)) {
					GlobalVar.CPNum = txtVal.getText().toString();
					if (GlobalVar.menu.equals("Code")) {
						QuestionDialog();
					} else
						new CustomDialog(context, CustomDialog.PIN).start();
				} else if (name.equals(Amount)) {
					new CustomDialog(context, CustomDialog.CPNumber).start();
				} else if (name.equals(IDNumber)) {
					GlobalVar.ID = txtVal.getText().toString();
					edit.putString("saveID", GlobalVar.ID);
					edit.commit();
					Toast.makeText(getContext(), "ID Number Saved",
							Toast.LENGTH_SHORT).show();
				} else if (name.equals(Gateway)) {
					GlobalVar.Gateway = txtVal.getText().toString();
					edit.putString("saveGate", GlobalVar.Gateway);
					edit.commit();
					Toast.makeText(getContext(), "Gateway Saved",
							Toast.LENGTH_SHORT).show();
				} else if (name.equals(PIN)) {
					SmsManager smsManager = SmsManager.getDefault();
					GlobalVar.PIN = txtVal.getText().toString();
					if (GlobalVar.menu.equals("Code")) {
						LoadMenuCode();
					} else if (GlobalVar.menu.equals("OTA")) {
						LoadMenuOTA();
					} else if (GlobalVar.menu.equals("Transfer")) {

					} else if (GlobalVar.menu.equals("Reload")) {
						Reload(GlobalVar.BankName);
					}
					smsManager.sendTextMessage(GlobalVar.Gateway, null, msg,
							null, null);
					Toast.makeText(getContext(), "Message Sent!",
							Toast.LENGTH_SHORT).show();
				}
				dialog.dismiss();
			}
		});
		btn_cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	private void QuestionDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage("Do you want to load another one right away?")
				.setTitle("Another?").setCancelable(false);
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getContext(), "Feature not yet available",
						Toast.LENGTH_SHORT).show();
				// dialog.dismiss();
			}
		});
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				new CustomDialog(getContext(), CustomDialog.PIN).start();
			}
		});
		builder.setCancelable(false);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	private void LoadMenuCode() {
		int index = ProductList.productsLoad[GlobalVar.numlist].indexOf("-");
		String CodeName = ProductList.productsLoad[GlobalVar.numlist]
				.substring(0, index);
		msg = "LX LOAD " + GlobalVar.ID + " " + GlobalVar.PIN + " " + CodeName
				+ " " + GlobalVar.CPNum;
	}

	private void LoadMenuOTA() {
		String CodeName = ProductList.productsLoad[GlobalVar.numlist];
		msg = "LX OTA " + GlobalVar.ID + "/" + GlobalVar.PIN + "/" + CodeName
				+ "/" + GlobalVar.amount + "/" + GlobalVar.CPNum;
	}

	private void Reload(String bank) {
		String accountnum;
		String msg1 = null;
		if (bank.equals("BDO")) {
			accountnum = "4640062675";
			msg1 = bank + "BDO/ABD Avenue Ortigas/" + accountnum;
		} else if (bank.equals("BPI")) {
			accountnum = "4021011091";
			msg1 = "BPI/Robinsons Tower Ortigas/" + accountnum;
		} else {
			accountnum = "007261503383";
			msg1 = bank + "METROBANK/H.V. Dela Costa/" + accountnum;
		}
		msg = "LX RELOAD " + GlobalVar.ID + "/" + GlobalVar.PIN + "/"
				+ GlobalVar.amount + "/" + msg1 + "/" + GlobalVar.month + "/"
				+ GlobalVar.day + "/" + GlobalVar.year + "/" + GlobalVar.hour
				+ ":" + GlobalVar.minute + ":" + GlobalVar.second;
	}
}
